var app = getApp();
Page({

  data: {
    info:[],
    userInfo: null,
    title:'',
    zhicheng:'',
    lid:'9999999',
    
  },

  onLoad: function (options) {
  
    this.setData({ lid: options.id });
    this.getlist(options.id)
  
  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{

      this.setData({ userInfo:wx.getStorageSync('userInfo')});

      this.setData({ title:wx.getStorageSync('title')});

      if(wx.getStorageSync('gerenziliao')){

        this.setData({ userinfo:wx.getStorageSync('gerenziliao')});
      }
    

  
    }
  },
  tongguo:function(){
    var that = this
    wx.showModal({
      title: '',
      content: '是否确认操作',
      success(res) {
       if (res.confirm) {
        that.shenhe(1)
       } else if (res.cancel) {
        console.log('用户点击取消')
       }
      }
  })
  },

  butongguo:function(){
    var that = this
    wx.showModal({
      title: '',
      content: '是否确认操作',
      success(res) {
       if (res.confirm) {
         that.shenhe(2)
       } else if (res.cancel) {
        console.log('用户点击取消')
       }
      }
  })
  },
  shenhe: function (event) {
      console.log(event)
      var that = this 
      wx.request({
        url: app.apiUrl + '/api/data/docpc',
        data: {
          cpid: that.data.lid,
          groupid:wx.getStorageSync('groupid'),
          openid:wx.getStorageSync('openid'),
          status:event
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res=> {
      
         if(res.data.code == 1){
  
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
          
          setTimeout(function () {
            wx.navigateTo({
              url: '/page/my/index',
            });
          }, 2000)
         }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
         }
  
        }
      })

  },

  formSubmit: function (event) {
    var that = this;
    if (event.detail.value.shuliang > 0) {
     
    }else{
      app.error('请输入正确的退料数量');
      return;
    }

    wx.request({
      url: app.apiUrl + '/api/data/fgcomproduce',
      data: {
        wid: that.data.lid,
        groupid:wx.getStorageSync('groupid'),
        openid:wx.getStorageSync('openid'),
        count:event.detail.value.shuliang
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
    
       if(res.data.code == 1){

        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        });
        
        setTimeout(function () {
          wx.navigateTo({
            url: '/page/my/index',
          });
        }, 2000)
       }else{
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        });
       }

      }
    })

  },

  getlist:function (lid){
    // var lid = 1

    var that = this
    wx.request({
      url: app.apiUrl + '/api/data/cpcdetail',
      data: {
        cpid:lid,
        openid:wx.getStorageSync('openid'),
        groupid:wx.getStorageSync('groupid')
        
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
  
     
        if(res.data.code == 1){
          that.setData({ info: res.data.data});
              
        }else{
          wx.showModal({
            title: '温馨提示',
               content: res.data.msg,
                 showCancel: false
            });
        }
     
      }
    });
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})